//
//  PopoverController.swift
//  nimabi
//
//  Created by danny on 15/7/3.
//  Copyright © 2015年 DannyLiu. All rights reserved.
//

import UIKit

class PopoverController: UIPresentationController {

    
    override init(presentedViewController: UIViewController, presentingViewController: UIViewController) {
        super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
        
        
        
        
    }
    
    /// 懒加载遮罩视图
    lazy var dummyVie: UIView = {
        
        let v = UIView()
        
        v.backgroundColor = UIColor(white: 0.0, alpha: 0.2)

        return v
    }()
    
    ///  给遮罩视图添加点击手势
    private func prepareDummyView(){
        
        let tap = UITapGestureRecognizer(target: self, action: "close")
        dummyVie .addGestureRecognizer(tap)
        
    }
    
    func close(){
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
    
    
    override func containerViewWillLayoutSubviews() {
        
        // 准备遮罩View
        prepareDummyView()
        
        // 添加并设置遮罩
        dummyVie.frame = containerView!.bounds
        // 直接插入到底层
        containerView?.insertSubview(dummyVie, atIndex: 0)
        
        // 设置被展现出来的视图的大小
        presentedView()?.frame = CGRectMake(100, 56, 200, 240)
        
    }
    
}
